package com.sxbbc.common.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.common.basics.exceptions.ThrowJsonException;
import com.sxbbc.common.basics.utils.date.DateUtil;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.dto.jz.SeckillTimeVo;
import com.sxbbc.common.core.entity.jz.ActivitySeckill;
import com.sxbbc.common.core.entity.jz.ActivitySeckillDetail;
import com.sxbbc.common.core.entity.jz.ProSkuDetail;
import com.sxbbc.common.core.mapper.jz.ActivitySeckillMapper;
import com.sxbbc.common.core.service.IActivitySeckillDetailService;
import com.sxbbc.common.core.service.IActivitySeckillService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxbbc.common.core.service.IProSkuDetailService;
import com.sxbbc.common.core.service.system.redis.RedisSeckillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 秒杀活动主表 服务实现类
 * </p>
 *
 * @author leehao
 * @since 2019-06-13
 */
@Service
public class ActivitySeckillServiceImpl extends ServiceImpl<ActivitySeckillMapper, ActivitySeckill> implements IActivitySeckillService {

    @Autowired
    private IActivitySeckillDetailService activitySeckillDetailService;
    @Autowired
    private RedisSeckillService redisSeckillService;
    @Autowired
    private IProSkuDetailService proSkuDetailService;

    @Override
    public List<SeckillTimeVo> queryTodaySeckillList() {
        List<SeckillTimeVo> timeList = new ArrayList<>();
        Date today = new Date();
        Date tomorrow = DateUtil.getInstance().addDate(today, 1);
        //今日秒杀
        SeckillTimeVo todaySeckill = new SeckillTimeVo();
        todaySeckill.setTimeStr("今日");
        List<SeckillTimeVo> todayList = super.baseMapper.querySeckillListByTime(today);
        todaySeckill.setTimeList(todayList);
        //明日秒杀
        SeckillTimeVo tomorrowSeckill = new SeckillTimeVo();
        tomorrowSeckill.setTimeStr("明日");
        List<SeckillTimeVo> tomorrowList = super.baseMapper.querySeckillListByTime(tomorrow);
        tomorrowSeckill.setTimeList(tomorrowList);
        timeList.add(todaySeckill);
        timeList.add(tomorrowSeckill);
        return timeList;
    }

    @Override
    public boolean queryNowTimeExist(String startTime) {
        List<ActivitySeckill> list = super.baseMapper.queryNowTimeExist(startTime);
        return !CollectionUtils.isEmpty(list);
    }

    @Override
    public RestResponse handleSeckillStatus(ActivitySeckill seckill) {
        boolean handle = false;
        //更新秒杀主表
        handle = super.updateById(seckill);
        if (!handle) {
            throw new ThrowJsonException("更新秒杀活动信息表失败");
        }
        //更新秒杀子表
        QueryWrapper<ActivitySeckillDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("seckill_id",seckill.getId());
        List<ActivitySeckillDetail> seckillDetailList = activitySeckillDetailService.list(wrapper);
        if (!CollectionUtils.isEmpty(seckillDetailList)) {
            for (ActivitySeckillDetail seckillDetail : seckillDetailList) {
                seckillDetail.setStatus(StaticUtils.STATUS_NO);
                //redis清除
                redisSeckillService.removeSkDetail(seckillDetail.getSkuId());
                //商品详情更新
                ProSkuDetail proSkuDetail = proSkuDetailService.getById(seckillDetail.getSkuId());
                proSkuDetail.setSeckillId(null);
                handle = proSkuDetailService.updateById(proSkuDetail);
            }
            if (!handle) {
                throw new ThrowJsonException("更新商品详情信息失败");
            }
            handle = activitySeckillDetailService.updateBatchById(seckillDetailList);
            if (!handle) {
                throw new ThrowJsonException("更新秒杀详情表失败");
            }
        }
        return GetRest.getSuccess("操作成功");
    }
}
